<?php

define(LOG_DIR, S_ROOT.'cache/');

# 排重
function check_record($appid, $idfa){
    //判断是否来自积分墙
    $if_kdrecord = getonerecord("SELECT * FROM tttj.jfq_kdrecord WHERE appid = '{$appid}' AND idfa = '{$idfa}' LIMIT 1");
    if(!empty($if_kdrecord)){
        return false;
    }else{
        $iff_kdrecord = getonerecord("SELECT * FROM tttj.jfq_kdrecord WHERE appid != '{$appid}' AND idfa = '{$idfa}' LIMIT 1");
        if(empty($iff_kdrecord)){
            //判断是否注册
            $if_userinfo = getonerecord("SELECT * FROM tttj.userinfo WHERE imei = '{$idfa}' LIMIT 1");
            if(empty($if_userinfo)){
                return true;
            }else{
                return false;
            }
        }else{
            return true;//来自积分墙的idfa在不同appid中都算激活
        }
    }
}


//单例模式的数据库操作函数 - 本地数据库
function getinstance0(){
    require_once(S_ROOT .'source/libs/Db0.class.php');
    return Dbhelper0::getinstance();
}
function getonerecord($query){
    $db = getinstance0();
    return $db->getonerecord($query);
}
function exec_db_insertid($query){
    $db = getinstance0();
    return $db->exec_db_insertid($query);
}
function exec_db($query){
    $db = getinstance0();
    return $db->exec_db($query);
}
function getarrrecord($query){
    $row = exec_db($query);
    while($val = mysql_fetch_assoc($row)){
        $list[] = $val;
    }
    return $list;
}

# json 返回
function echojson($msg = '', $status = 0, $dataarr = array()) {
    $arr = array (
        'msg'    => $msg,
        'status' => $status.''
    );
    if(!empty($dataarr)){
        $arr['data'] = $dataarr;
    }
	echo json_encode($arr);
    exit;
}

# 日志函数
function dolog($msg='',$datas=''){
    $time = date('H:i:s',time());
    $filename = LOG_DIR.'jfq_'.date('Y-m-d',time()).'.log';
    if(is_array($datas)){
        file_put_contents($filename, "[{$time}]\n"."{$msg}\n".var_export($datas,true)."\n\n", FILE_APPEND);
    }else{
        file_put_contents($filename, "[$time]\n"."{$msg}\n".$datas."\n\n", FILE_APPEND);
    }
}

# 过滤参数
function inputFilter($str)
{
   if(empty($str))
   {
       return $str;
   }
   $str=trim($str);
   // $str=str_ireplace("&","&amp;",$str);
   // $str=str_ireplace(">","&gt;",$str);
   // $str=str_ireplace("<","&lt;",$str);
   // $str=str_ireplace(chr(34),"&",$str);
   // $str=str_ireplace(chr(39),"&#39;",$str);
   // $str=str_ireplace(chr(13),"<br />",$str);
   // $str=str_ireplace("'","''",$str);
   $str=str_ireplace("select","sel&#101;ct",$str);
   $str=str_ireplace("join","jo&#105;n",$str);
   $str=str_ireplace("union","un&#105;on",$str);
   $str=str_ireplace("where","wh&#101;re",$str);
   $str=str_ireplace("insert","ins&#101;rt",$str);
   $str=str_ireplace("delete","del&#101;te",$str);
   $str=str_ireplace("update","up&#100;ate",$str);
   $str=str_ireplace("like","lik&#101;",$str);
   $str=str_ireplace("drop","dro&#112;",$str);
   $str=str_ireplace("create","cr&#101;ate",$str);
   $str=str_ireplace("modify","mod&#105;fy",$str);
   $str=str_ireplace("rename","ren&#097;me",$str);
   $str=str_ireplace("alter","alt&#101;r",$str);
   $str=str_ireplace("cast","ca&#115;",$str);
   return $str;
}
